<?php
include_once($_SERVER["DOCUMENT_ROOT"].'/inc/config.inc.php');

if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
    header('Content-Type: application/x-javascript; charset=ISO-8859-1');
} else {
    header("Content-type: text/html; charset=utf8");
}

function json_decode2($content, $assoc=false) {
    require_once($_SERVER["DOCUMENT_ROOT"].'/lib/JSON.php');
    if ($assoc){
        $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
    } else {
        $json = new Services_JSON;
    }
    return $json->decode($content);
}

function json_encode2($content) {
    require_once($_SERVER["DOCUMENT_ROOT"].'/lib/JSON.php');
    $json = new Services_JSON;
    return $json->encode($content);
}

// getting key-value-pair from GET request
$qs = '';

foreach($_GET as $key) {
    $qs .= key($_GET);
    $qs .= "=";
    $qs .= $key;
    $qs .= "&";
    next($_GET);
}

// requesting JSON response from API using curl
$url = $wwwroot.'/api/fixturesAPI.php?' . $qs;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

if ($result) {
    //echo json_decode($result);
    $games = json_decode2($result);
} else {
    echo "sorry, no results returned. must be something wrong the the request";   
}

//print_r($games);

if (sizeof($games) > 0) {
    
    // set the locale correctly
    switch ($_GET['lang']) {
        case 'de':
            setlocale (LC_ALL, 'de_DE', 'de_DE', 'de', 'ge');
        break;
        default:
            setlocale (LC_ALL, 'en_EN', 'en_EN', 'en', 'en');
    }
    
    $nbr_of_games = sizeof($games);
    
    // put together event table
    $markup = '';
    $markup .= '<table cellspacing="0" summary="'.$nbr_of_games.' games listed, starting with '.$games[0]->team_a.' v '.$games[0]->team_b.' on '.date("D d F y",strtotime($games[0]->kickoff)).' at '.date("g:iA",strtotime($games[0]->kickoff)).' in '.$games[0]->city.'">';
    $markup .= '<caption>Euro KickOff Fixtures</caption>';
    $markup .= '<thead>';
    $markup .= '<tr>';
    $markup .= '<th id="match_date" scope="col">Date</th>';
    $markup .= '<th abbr="Kick Off" id="match_time" scope="col">Kick Off <span class="gmt">(GMT)</span></th>';
    $markup .= '<th id="match_teams" scope="col">Teams</th>';
    $markup .= '<th id="match_venue" scope="col">Venue</th>';
    $markup .= '</tr>';
    $markup .= '</thead>';
    
    $markup .= '<tfoot>';
    $markup .= '<tr>';
    $markup .= '<td colspan="5">'.sizeof($games).' games listed.</td>';
    $markup .= '</tr>';
    $markup .= '</tfoot>';
    
    $markup .= '<tbody>';
    
    foreach ($games as $game) {
        $markup .= '<tr id="EUKO-'.$game->game_id.'" title="EUKO-'.$game->game_id.'" class="vevent uid">';
            $markup .= '<td headers="match_date"><abbr title="'.date("Y-m-d\TH:i",strtotime($game->kickoff)).'+0000" class="dtstart dtstamp">'.strftime("%A, %B %#d", strtotime($game->kickoff)).'</abbr></td>';
            $markup .= '<td headers="match_time"><abbr title="'.date("Y-m-d\TH:i",strtotime($game->kickoff)+7200).'+0000" class="dtend">'.strftime("%H:%M", strtotime($game->kickoff)).'</abbr></td>';
            $markup .= '<td class="summary uid" headers="match_teams">'.$game->team_a.' '.$game->goals_a.'-'.$game->goals_b.' '.$game->team_b.'</td>';
            $markup .= '<td headers="match_venue">';
                $markup .= '<span class="location">';
                $markup .= $game->venue.', '.$game->city;
                // GEODATA for event, for later
                // $markup .=  '<span class="geo">';
                // $markup .=  '(Geo: ';
                // $markup .=  '<span class="latitude">51.4559</span>,';
                // $markup .=  '<span class="longitude">-0.3416</span>)';
                // $markup .=  '</span>';
                $markup .= '</span>';
            $markup .= '</td>';
        $markup .= '</tr>';
    }

    $markup .= '</tbody>';
    $markup .= '</table>';

    // List version.
    $markup = '<ol class="fixtures-list">';
    
    $count = 1;
    foreach ($games as $game) {
      $markup .= '<li id="euko-'.$game->game_id.'" title="euko-'.$game->game_id.'" class="vevent uid' . (( $count % 2 == 0 ) ? ' even' : ' odd') . '">';
          $markup .= '<h3 class="summary uid"><span class="team-a">'.$game->team_a.'</span> <span class="splitter">'. (($game->goals_a != '' && $game->goals_b != '') ? $game->goals_a .'-'.$game->goals_b : '&mdash;') .'</span> <span class="team-b">'.$game->team_b.'</span></h3>';
          $markup .= '<p><abbr title="'.date("Y-m-d\TH:i",strtotime($game->kickoff)).'+0100" class="dtstart dtstamp">'.strftime("%A, %B %d", strtotime($game->kickoff)).'</abbr>';
          $markup .= ' <abbr title="'.date("Y-m-d\TH:i",strtotime($game->kickoff)+7200).'+0100" class="dtend">'.strftime("%H:%M", strtotime($game->kickoff)).'</abbr></p> ';
              $markup .= '<p class="location">';
              $markup .= $game->venue.', '.$game->city;
              // GEODATA for event, for later
              // $markup .=  '<span class="geo">';
              // $markup .=  '(Geo: ';
              // $markup .=  '<span class="latitude">51.4559</span>,';
              // $markup .=  '<span class="longitude">-0.3416</span>)';
              // $markup .=  '</span>';
			  
          $markup .= '</p>';
		  $markup .= '<p class="description">'. $game->stage .'</p>';
      $markup .= '</li>
      ';
      $count++;
    }
    $markup .= '</ol>';

    echo $markup;
}
?>